package com.automannn.practice.mybatis.pureEnvironment;

import com.automannn.practice.mybatis.dao.PersonDao;
import com.automannn.practice.mybatis.entity.PersonEntity;
import org.apache.ibatis.builder.StaticSqlSource;
import org.apache.ibatis.datasource.pooled.PooledDataSource;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.mapping.*;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;

/**
 * @author chenkh
 * @time 2021/10/25 9:49
 */
public class MybatisTest {
    public static void main(String[] args) throws IOException {
        //使用XML的方式构建 SqlSessionFactory
        String resource = "org/mybatis/example/mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        //设置properties有点问题，未被正常解析
//        Configuration configuration= sqlSessionFactory.getConfiguration();
//        Properties properties = new Properties();
//        properties.setProperty("driver","com.mysql.cj.jdbc.Driver");
//        properties.setProperty("url","jdbc:mysql://localhost:3306/zhenhe?useUnicode=true&serverTimezone=CST&characterEncoding=utf-8");
//        properties.setProperty("username","root");
//        properties.setProperty("password","chenkaihai");
//        configuration.setVariables(properties);

        //使用 java编码的方式构建 SqlSessionFactory
//        PooledDataSource dataSource = new PooledDataSource();
//        TransactionFactory transactionFactory = new JdbcTransactionFactory();
//        dataSource.setDriver("com.mysql.cj.jdbc.Driver");
//        dataSource.setUrl("jdbc:mysql://localhost:3306/zhenhe?useUnicode=true&serverTimezone=CST&characterEncoding=utf-8");
//        dataSource.setUsername("root");
//        dataSource.setPassword("chenkaihai");
//
//        Environment environment = new Environment("automannn",transactionFactory,dataSource);
//        Configuration configuration = new Configuration(environment);
//        configuration.addMapper(PersonDao.class);
//        SqlSource sqlSource = new StaticSqlSource(configuration,"select * from person");
//        ResultMapping resultMapping1 = new ResultMapping.Builder(configuration,"pid","pid",String.class).build();
//        ResultMapping resultMapping2 = new ResultMapping.Builder(configuration,"pname","pname",String.class).build();
//        List<ResultMapping> mappingList = new ArrayList<>();
//        mappingList.add(resultMapping1);
//        mappingList.add(resultMapping2);
//        ResultMap resultMap = new ResultMap.Builder(configuration,"com.automannn.practice.mybatis.dao.PersonDao.selectList", PersonEntity.class,mappingList).build();
//        MappedStatement mappedStatement = new MappedStatement.Builder(configuration,"com.automannn.practice.mybatis.dao.PersonDao.selectList",
//                sqlSource, SqlCommandType.SELECT).resultMaps(Arrays.asList(resultMap)).build();
//        configuration.addMappedStatement(mappedStatement);
//
//        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);

        //xml定义语句
        SqlSession sqlSession = sqlSessionFactory.openSession();
        PersonDao personDao = sqlSession.getMapper(PersonDao.class);
//        System.out.println(personDao.selectList());

        //注解定义语句
//        System.out.println(personDao.queryAll());

        PersonEntity personEntity = new PersonEntity();
        personEntity.setPname("bbb");

        System.out.println(personDao.selectBySingleParam("abcsd"));
    }
}
